import numpy as np
'''统一各类神煞的函数'''
def calculate(zhiMasked: np.array, actZhi, calculMethod: str = '+', pattern: str = '', group_nums: int = 4):
    '''

    - 周期为4,逆行四仲
    calcul:add(逆行),T:4(周期为4),四仲由结果指定
    ['亥卯未', '申子辰', '巳酉丑', '寅午戌'][gua_dict['d'] % 4]
    亥卯未日,沐浴在子;申子辰日,沐浴在酉

    - 周期为6,顺行6阳辰
    calcul:minus(顺行),T:6(周期为6)
    ['亥子丑寅卯辰','巳午未申酉戌']
    *天马：正月起午，顺行六阳辰，主迁动诏命出行之事。*
    *皇恩：正月起未，顺行六阴辰，主皇恩庇护。*

    - 周期为12,寅字起亥逆行(六合)
    calcul:add(逆行),T:6(周期为6)
    *月厌：正月起戌逆行十二辰。月厌妨嫁娶，加玄盗贼，加蛇怪梦，加虎克日病死。加朱勾忧禁，逃者忌向此方。*
    calculate(zhiMasked,gua.m,'add','divide')

    - 周期为12,相邻3期一致
    天喜：春戌夏丑秋辰冬未也。主喜庆、恩泽、官迁、财喜。
    calculate(zhiMasked,gua.m,'diff','divide')

    - 神煞种类估计
    (+-)*[(''or 'divide' or 'mod')*(6,4,3,2) + 'divide2']*(和差结果有0-11种可能取值)
    2*(3*4+1)*12 = 26*12 = 312种可能取值

    :param zhiMasked:
    :param actZhi:
    :param calculMethod: '+/sum/add','-/diff/minus'
    :param pattern: ''or 'divide' or 'mod' or 'divide2'
    :param group_nums: 分多少组,6,4,3,2.如 group_nums:4,也就是说"亥卯未","申子辰","巳酉丑","寅午戌"四组
    :return:
    '''
    assert isinstance(group_nums, int) and group_nums <= 12, "周期T应该是小于等于12的整数"
    step = 12 // group_nums

    # 只有顺行或逆行的区别:子丑寅卯辰巳午未,亥戌酉申未午
    # 4群:(亥子丑)戌戌戌,(寅卯辰)丑丑丑,辰辰辰,未未未;6群:丑丑,卯卯,巳巳;3群:亥亥亥亥,卯卯卯卯,未未未未
    # 4群:(亥卯未)子,(申子辰)酉,(巳酉丑)午,(寅午戌)卯,为沐浴;(寅申巳亥)子,(子午卯酉)辰,(辰戌丑未)申;
    if not pattern:
        x_0 = actZhi
    elif pattern == 'divide':
        x_0 = actZhi // step * step
    elif pattern == 'divide2':
        x_0 = (actZhi + 1) // 2 * 2 + (actZhi + 1) % 2 * 6
    else:
        # day_status = (zhi_mask + gua_dict['d'] % 4 * 3) % 12
        x_0 = actZhi % group_nums * step

    # +为逆行,-为顺行
    if calculMethod in ['add', 'sum', '+']:
        return (zhiMasked + x_0) % 12
    elif calculMethod in ['minus', 'diff', '-']:
        return (zhiMasked - x_0 + 12) % 12
    else:
        raise ValueError(calculMethod, 'not valid')

if __name__ == '__main__':
    # 示例：周期为4，逆行四仲
    zhiMasked = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])  # 地支索引
    actZhi = 0  # 当前活动地支索引（子）
    result = calculate(zhiMasked, actZhi, calculMethod='+', pattern='', group_nums=4)
    print("周期为4，逆行四仲的结果：", result)

    # 示例：周期为6，顺行6阳辰
    result = calculate(zhiMasked, actZhi, calculMethod='-', pattern='', group_nums=6)
    print("周期为6，顺行6阳辰的结果：", result)

    # 示例：周期为12，寅字起亥逆行
    result = calculate(zhiMasked, actZhi, calculMethod='+', pattern='divide', group_nums=12)
    print("周期为12，寅字起亥逆行的结果：", result)